button.btn.btn-social.btn-facebook(onclick='facebookLogin()') Facebook
script(type='text/javascript').
  function facebookLogin() {
    var FB = window.FB;
    var facebookScope = '#{stormpathConfig.web.social.facebook.scope}';

    FB.login(function (response) {
      if (response.status === 'connected') {
        var queryStr = window.location.search.replace('?', '');
        // TODO make dynamic
        if (queryStr) {
          // Don't include any access_token parameters in
          // the query string as it will be added by us.
          queryStr = queryStr.replace(/(&?)access_token=([^&]*)/, '');

          window.location.replace('/callbacks/facebook?' + queryStr + '&access_token=' + response.authResponse.accessToken);
        } else {
          window.location.replace('/callbacks/facebook?access_token=' + response.authResponse.accessToken);
        }
      }
    }, {scope: facebookScope});
  }

  window.fbAsyncInit = function () {
    FB.init({
      appId      : '#{stormpathConfig.web.social.facebook.clientId}',
      cookie     : true,
      xfbml      : true,
      version    : 'v2.3'
    });
  };

  (function (d, s, id){
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));
